package sangpil.motionGuide
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import gs.TweenLite;
	import gs.easing.*;
	
	import sangpil.motionGuide.controller.ControllerTest;
	import sangpil.motionGuide.controller.event.ControlEvent;
	import sangpil.motionGuide.controller.event.ControlReset;
	import sangpil.motionGuide.controller.event.ControlUpdate;
	import sangpil.motionGuide.controller.event.ControlUpdateAll;
	import sangpil.motionGuide.viewer.SetTest;
	import sangpil.motionGuide.viewer.event.ViewerEvent;

	public class GuideSet extends Sprite
	{
		public function GuideSet()
		{
			super();
			this.setLayout();
		}
		
		private var setTest:SetTest;
		private var control:ControllerTest;
		private function setLayout():void
		{
			this.setTest = new SetTest();
			this.addChild(this.setTest);
			this.setTest.addEventListener(ViewerEvent.SEND_OBJECT, receiveInfo);
			this.setTest.x = 480;
			
			this.control = new ControllerTest();
			this.control.addEventListener(ControlEvent.EXPORT_INFO, setInfo);
			this.control.addEventListener(ControlUpdate.UP_DATE, updateInfo);
			this.control.addEventListener(ControlUpdateAll.RUN_All, easingAll);
			this.control.addEventListener(ControlReset.RESET_ALL, resetAll);
			this.addChild(this.control);
			this.control.x = 0;
		}
		
		private function easingAll(e:ControlUpdateAll):void
		{
			var dO:Object = new Object();
			dO.id = e.objectDefault.id;
			dO.name = e.objectDefault.name;
			dO.saved = false;
			dO.activate = false;
			dO.delay = e.objectDefault.delay;
			dO.duration = e.objectDefault.duration;
			dO.ease = e.objectDefault.ease;
			dO.easeData = e.objectDefault.easeData;
			this.setTest.totalArr[e.objectDefault.id].defaultInfo = dO;
	
			
			var eO:Object = new Object();
			eO.x = e.objectEnd.x;
			eO.y = e.objectEnd.y;
			eO.scale = e.objectEnd.scale;
			eO.rotation = e.objectEnd.rotation;
			eO.alpha = e.objectEnd.alpha;
			this.setTest.totalArr[e.objectDefault.id].endInfo = eO;
			
			for(var i:uint = 0; i< this.setTest.totalArr.length; i++ )
			{
				this.runOne(this.setTest.totalArr[i]);
			}
		}
		
		private function resetAll(e:ControlReset):void
		{
			for(var i:uint = 0; i< this.setTest.totalArr.length; i++ )
			{
				this.onFinishTween(this.setTest.totalArr[i]);
			}
		}

		private function updateInfo(e:ControlUpdate):void
		{
			var dO:Object = new Object();
			dO.id = e.objectDefault.id;
			dO.name = e.objectDefault.name;
			dO.saved = false;
			dO.activate = false;
			dO.delay = e.objectDefault.delay;
			dO.duration = e.objectDefault.duration;
			dO.ease = e.objectDefault.ease;
			dO.easeData = e.objectDefault.easeData;
			this.setTest.totalArr[e.objectDefault.id].defaultInfo = dO;
	
			
			var eO:Object = new Object();
			eO.x = e.objectEnd.x;
			eO.y = e.objectEnd.y;
			eO.scale = e.objectEnd.scale;
			eO.rotation = e.objectEnd.rotation;
			eO.alpha = e.objectEnd.alpha;
			this.setTest.totalArr[e.objectDefault.id].endInfo = eO;
		}
		
		private function setInfo(e:ControlEvent):void
		{
			var dO:Object = new Object();
			dO.id = e.objectDefault.id;
			dO.name = e.objectDefault.name;
			dO.saved = false;
			dO.activate = false;
			dO.delay = e.objectDefault.delay;
			dO.duration = e.objectDefault.duration;
			dO.ease = e.objectDefault.ease;
			dO.easeData = e.objectDefault.easeData;
			this.setTest.totalArr[e.objectDefault.id].defaultInfo = dO;
	
			
			var eO:Object = new Object();
			eO.x = e.objectEnd.x;
			eO.y = e.objectEnd.y;
			eO.scale = e.objectEnd.scale;
			eO.rotation = e.objectEnd.rotation;
			eO.alpha = e.objectEnd.alpha;
			this.setTest.totalArr[e.objectDefault.id].endInfo = eO;
			
			
			this.runOne(this.setTest.totalArr[e.objectDefault.id]);
/* 			TweenLite.to(this.setTest.totalArr[e.objectDefault.id], e.objectDefault.duration,
			 {x:e.objectEnd.x, y:e.objectEnd.y,
			// ease:arg3, 
			 delay:e.objectDefault.delay
			 //onComplete:onFinishTween, 
			 //onCompleteParams:[2, this.rect, arg3]
			 }); */
		}
		
		private function runOne(mc:MovieClip):void
		{
			TweenLite.to(
						mc, 
						mc.defaultInfo.duration,
						
						 {x:mc.endInfo.x, y:mc.endInfo.y,
						 scaleX:mc.endInfo.scale, scaleY:mc.endInfo.scale,
						 rotation:mc.endInfo.rotation,
						 alpha:mc.endInfo.alpha,
						 delay:mc.defaultInfo.delay,
						 ease:mc.defaultInfo.easeData/*, 
						 onComplete:onFinishTween,
						 onCompleteParams:[mc] */
			 }); 
		}
		
		private function onFinishTween(mc:MovieClip):void
		{
			mc.x = mc.startInfo.x;
			mc.y = mc.startInfo.y;
			mc.alpha = 1;
			mc.rotation = 0;
			mc.scaleX = 1;
			mc.scaleY = 1;
		}
		
		private function receiveInfo(e:ViewerEvent):void
		{
			this.control.updateInfo(e.objectDefault, e.objectStart, e.objectEnd);
		}
	}
}